package com.help10000.rms.ui.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.amir.common.mvp.base.AbstractBaseFragment;
import com.amir.common.mvp.presenter.IPresenter;
import com.amir.common.mvp.view.IView;
import com.amir.common.utils.AppManager;
import com.amir.common.utils.SPUtils;
import com.help10000.rms.ui.activitys.logins.LoginActivity;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;

import org.jetbrains.annotations.Nullable;


public abstract class BaseFragment<P extends IPresenter, V extends IView> extends AbstractBaseFragment<P, V> {

    private BasePopupView basePopupView;
    private boolean isLazied = false;
//    @Override
//    public void onError(String string) {
//        Message message = new Message();
//        message.obj = string;
//        mHandler.sendMessage(message);
//        String[] array = {"", ""};
//        List<String> list = new ArrayList<String>(array.length);
//    }


    @Override
    public void onResume() {
        super.onResume();
        if (!isLazied && isVisible()) {
            isLazied = true;
            lazyLoad();
        }
    }

    protected void lazyLoad() {

    }

    @Override
    public void showDialog(@Nullable String mess, int code) {
        if (basePopupView != null) {
            basePopupView.dismiss();
            basePopupView.destroy();
        }
        basePopupView = new XPopup.Builder(getViewContext())
                .asConfirm("出错啦", mess, null, "确定", () -> {
                        }, null, true
                ).show();
    }

    @Nullable
    @Override
    public Context getViewContext() {
        return mContext;
    }

    @Override
    public void onAgainLogin() {
        mHandler.sendEmptyMessage(1);
    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                Toast.makeText(mContext, (String) msg.obj, Toast.LENGTH_SHORT).show();
            } else {
                if (isAdded()) {
                    SPUtils.Companion.removeFile();
                    AppManager.getAppManager().finishAllActivity();
                    startActivity(new Intent(mContext, LoginActivity.class));
                }

            }
        }
    };
}
